var isAdd = false;
var presentRecordGrid, presentRecordStore, presentRecordDialog;
$(function () {
    initGrid();
    initDialog();
    initCombo()

});
function initCombo() {
    var data = [{id: '未审核', name: '未审核'}, {id: '完成提现', name: '完成提现'}, {id: '提现失败', name: '提现失败'}]
    CommonClass.setComboData($('.state1'), data, "id", "name");
    CommonClass.setComboData($('.state'), data, "id", "name", "All");
    CommonClass.AjaxAction(ctx + "/user/admin/getAllUsers", {}, function (data) {
        CommonClass.setComboData($('.userId'), data, "id", "userName");

    })
}
function initGrid() {

    var Grid = BUI.Grid,
        Store = BUI.Data.Store,
        Format = Grid.Format,
        columns = [
            {title: '提现ID', width: 100, dataIndex: 'id'},
            {title: '用户id', width: 100, dataIndex: 'userId'},
            {title: '用户名', width: 100, dataIndex: 'userName'},
            {title: '银行名称', width: 100, dataIndex: 'bankName'},
            {title: '卡号', width: 100, dataIndex: 'carNo', showTip: true},
            {title: '提现金额', width: 100, dataIndex: 'amount'},
            {title: '提现时间', width: 100, dataIndex: 'presentTime'},
            {title: '状态', width: 100, dataIndex: 'state'}
        ];

    presentRecordStore = new Store({
        url: ctx + "/presentRecord/admin/selectPageInfo",
        autoLoad: true,
        root: 'list',               //存放数据的字段名(rows)
        totalProperty: 'total',
        pageSize: 10	// 配置分页数目
    });
    presentRecordGrid = new Grid.Grid({
        render: '#presentRecordGrid',
        columns: columns,
        loadMask: true,
        forceFit: true,
        store: presentRecordStore,
        bbar: {
            // pagingBar:表明包含分页栏
            pagingBar: true
        },
        plugins: [Grid.Plugins.CheckSelection, Grid.Plugins.AutoFit], //勾选插件、自适应宽度插件
        tbar: {
            items: [{
                btnCls: 'button button-primary button-small',
                text: '新增',
                handler: addPresentRecord
            }, {
                btnCls: 'button button-primary button-small',
                text: '修改',
                handler: editPresentRecord
            }, {
                btnCls: 'button button-primary button-small',
                text: '删除',
                handler: deletePresentRecord
            }]
        }
    });
    presentRecordGrid.render();
    //创建表单，表单中的日历，不需要单独初始化

    BUI.use(['bui/grid', 'bui/data', 'bui/form'], function (Grid, Data, Form) {
        var form = new Form.HForm({
            srcNode: '#presentSearchForm'
        });
        form.on('beforesubmit', function (ev) {
            //序列化成对象
            var obj = form.serializeToObject();
            obj.start = 0; //返回第一页
            presentRecordStore.load(obj);
            return false;
        });
        form.render();
    })

    presentRecordStore.load();
}
function initDialog() {
    presentRecordEditForm = Bui.formRender("presentRecordEditForm");

    presentRecordDialog = Bui.overlay({
        id: 'presentRecordEditFormDialog',
        title: '修改提现信息',
        width: 500,
        height: 350,
        //配置DOM容器的编号
        contentId: 'presentRecordEditFormDialogContent',
        buttons: [
            {
                text: '保存',
                elCls: 'button button-primary',
                handler: function () {
                    if (!presentRecordEditForm.isValid()) {
                        return;
                    }
                    var formVal = presentRecordEditForm.serializeToObject();
                    if (isAdd) {
                        formVal.id = null;
                        formAction(formVal, ctx + "/presentRecord/admin/insertPresentRecord");
                    } else {
                        formAction(formVal, ctx + "/presentRecord/admin/updatePresentRecord");
                    }


                }
            }, {
                text: '关闭',
                elCls: 'button',
                handler: function () {
                    this.close();
                }
            }
        ]
    });
}
function addPresentRecord() {
    $('.presentTime').attr("disabled", false);
    $('.userId').attr("disabled", false);
    isAdd = true;
    presentRecordDialog.set("title", "新增提现信息");
    document.getElementById("presentRecordEditForm").reset();
    CommonClass.fieldVaild();
    presentRecordDialog.show();
}
function editPresentRecord() {
    var selections = presentRecordGrid.getSelection();
    isAdd = false
    if (selections.length == 0) {
        BUI.Message.Alert('请选择一条记录！', function () {
        }, 'info');
        return;
    }
    if (selections.length > 1) {
        BUI.Message.Alert('只能选择一条记录！', function () {
        }, 'info');
        return;
    }
    document.getElementById("presentRecordEditForm").reset();
    presentRecordEditForm.setRecord(selections[0]);
    $('.presentTime').attr("disabled", true);
    $('.userId').attr("disabled", true);

    presentRecordDialog.set("title", "修改提现信息");
    presentRecordDialog.show();

}
function deletePresentRecord() {
      CommonClass.deleteGridData(presentRecordGrid,presentRecordStore,ctx + "/presentRecord/admin/deletePresentRecord");

}
function formAction(formVal, actionUrl) {
    CommonClass.AjaxAction(actionUrl, formVal, function (data) {
        if (data.success) {
            Message.show(data.msg, "success", 2000);
            presentRecordStore.load()
            presentRecordDialog.close();
        } else {
            Message.show(data.msg, "error", 2000);
        }
    });
}